using System.Xml;

namespace IMSAT.OFTD.DOM
{
    public class ElementLink:Node
    {
        public Element Element{ get; private set;}
        internal string Id { get; set; }

        internal void SetelemsByID(Page pg)
        {

            CheckNodeDelegateChain delchain1 = new CheckNodeDelegateChain();
            delchain1.AddDelegate(delegate(Node obj)
            {
                if (obj is Element && (obj as Element).Id.OriginalId == Id)
                    return true;
                return false;
            });


            delchain1.Finish();
            NodeCollection<Node> res = pg.Find(delchain1);
            if (res.Count == 1)
                Element = res[0] as Element;
        }
        #region Nested type: ElementLinkXmlReader

        public class ElementLinkXmlReader : NodeXmlReader
        {
            protected override bool CheckReader(XmlReader rd, bool _prevRes, Node parentNode)
            {
                if (_prevRes)
                {
                    if (parentNode is Section)
                        return true;
                    return false;
                }
                return base.CheckReader(rd, _prevRes, parentNode);
            }

            protected override Node InitNode(XmlReader reader, Node initedNode)
            {
                if (initedNode == null)
                    initedNode = new ElementLink();
                var l = (ElementLink)initedNode;
                bool res = reader.MoveToFirstAttribute();
                while (res)
                {
                    if(reader.LocalName.Contains("ID_"))
                    {
                        l.Id = reader.Value;
                        break;
                    }
                    res = reader.MoveToNextAttribute();
                }
                reader.MoveToElement();
                return base.InitNode(reader,initedNode);
            }
        }

        #endregion
    }
}